//
//  CommonTool.m
//  Running
//
//  Created by 郑东喜 on 2016/10/30.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

#import "CommonTool.h"

#import "Encryption.h"
#import "WKWebViewController.h"
#import "MBManager.h"

#import "CountDown.h"

#import "LoginVC.h"

@implementation CommonTool



+ (void)alertWithTitle:(NSString *)title withMsg:(NSString *)msg {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
}

#pragma mark - lazy load
- (NetworkTools *)manager {
    if (!_manager) {
        _manager = [[NetworkTools alloc] init];
    }
    return _manager;
}

- (CountDown *)cBtn {
    if (!_cBtn) {
        _cBtn = [[CountDown alloc] init];
        _cBtn.delegate = self;
        
        //开始倒计时
        [_cBtn openCountdown];
    }
    return _cBtn;

}


- (void)sendAutoSEL:(UITextField *)tf {
    
    if ((tf.text.length == 0)) {
        [MBProgressHUD showError:@"手机号不能为空"];
        NSLog(@"%s  %d  ",__func__,__LINE__);
        return;
    }
    
    NSString *url = [[NSString stringWithFormat:@"http://bzg.ie1e.com/APP/User_UserLogin/DoCode?tel=%@",tf.text]
                     stringByAppendingString:@"&tys=1"];
    
    if ([Encryption checkTelNumber:tf.text] == true) {
        NSLog(@"%s  %d  %@",__func__,__LINE__,url);
//        [MBManager showBriefAlert:@"发送中..."];
        [MBProgressHUD showMessage:@"发送中..."];
        [self.manager requestWithMethod:post andUrlString:url andParameters:nil andFinished:^(id response, NSError *error) {
            if (!error) {
                NSLog(@"%s  %d  %@",__func__,__LINE__,response);
                
                if ([response[@"resultmsg"] isEqualToString:@"发送成功"]) {
                    [MBManager hideAlert];
                    [MBProgressHUD showSuccess:@"发送成功"];
                    
                    if ([self.delegate respondsToSelector:@selector(finishSendMsg:)]) {
                        [self.delegate finishSendMsg:self];
                    }
                    
#warning mark - 按钮相因时间
//                    [self cBtn];
                    
                } else if ([response[@"resultmsg"] isEqualToString:@"短信套餐失效"]) {
                    [MBManager hideAlert];
                    [MBProgressHUD showError:@"出现未知错误"];
                } else if ([response[@"resultmsg"] isEqualToString:@"请求失败"]) {
                    [MBManager hideAlert];
                    [MBProgressHUD showError:@"短信发送失败"];
                }
            } else {
                [MBManager hideAlert];
                [MBProgressHUD showError:@"网络异常"];
            }
        }];

    } else {
        [MBManager hideAlert];
        [MBProgressHUD showError:@"手机格式不对"];
    }
    
}

- (void)loginSEL:(UITextField *)numTF withPassTf:(UITextField *)passTF {
    
   __block BOOL loginSuccess;
    if (numTF.text.length == 0 ) {
        [MBProgressHUD showError:@"手机号不能为空"];
        return;
    }else if (passTF.text.length == 0 ){
        [MBProgressHUD showError:@"密码不能为空"];
        return;
    } else if ([Encryption checkTelNumber:numTF.text] == true) {

        NSLog(@"%s  %d  ",__func__,__LINE__);
            //http://bzg.ie1e.com/APP/User_UserLogin/Login?eid=微信号&tel=手机号&pwd=密码
            NSString *url = [NSString stringWithFormat:@"http://bzg.ie1e.com/APP/User_UserLogin/Login?tel=%@&pwd=%@",numTF.text,[Encryption md5EncryptWithString:passTF.text]];
            
            NSLog(@"%s  %d  %@",__func__,__LINE__,url);

//            [MBManager showBriefAlert:@"登陆中..."];
            [MBProgressHUD showMessage:@"登陆中..."];
            [self.manager requestWithMethod:post andUrlString:url andParameters:nil andFinished:^(id response, NSError *error) {
                

                if (!error) {
                    NSLog(@"%s  %d  %@",__func__,__LINE__,response);
                    
                    if ([response[@"resultmsg"] isEqualToString:@"成功消息返回"]) {
                        NSLog(@"%s  %d  %@",__func__,__LINE__,response[@"Data"][@"user"][0][@"uid"]);
                        
                        [stan setObject:numTF.text forKey:uName];
                        [stan setObject:passTF.text forKey:uPassword];
                        
                        [stan setObject:[NSString stringWithFormat:@"%@",response[@"Data"][@"user"][0][@"uid"]] forKey:userID];
                        
                        
                        NSLog(@"%s  %d  %@",__func__,__LINE__,[stan objectForKey:userID]);
                        
                        [stan synchronize];
                        
                        [MBManager hideAlert];
                        [MBProgressHUD showSuccess:@"成功"];
                        //延时进入游戏界面
                        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
                            
                            [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:[WKWebViewController new] animated:YES completion:nil];
                        });
                        
                        [stan setObject:[NSString stringWithFormat:@"%zd",loginSuccess] forKey:isLoginSuccess];
                        [stan synchronize];
                        
                    } else if([response[@"resultmsg"] isEqualToString:@"用户不存在"]) {
                        [MBManager hideAlert];
                        [MBProgressHUD showError:@"用户不存在"];
                        
                    } else if([response[@"resultmsg"] isEqualToString:@"手机已注册"]){
                        [MBManager hideAlert];
                        [MBProgressHUD showError:@"手机已注册"];
                    }else {
                        [MBManager hideAlert];
                        [MBProgressHUD showError:@"用户或密码不对"];
                    }
                }
                
                if (error) {
                    [MBManager hideAlert];
                    NSLog(@"%s  %d  %@",__func__,__LINE__,error.localizedDescription);
                    [MBProgressHUD showError:@"网络异常"];
                    
                }
            }
             
        ];
        
    
    } else {
        [MBManager hideAlert];
        [MBProgressHUD showError:@"手机格式不对"];
    }
    
}

- (void)forgetPassSELWith:(UITextField *)tfNum withTFpass:(UITextField *)passTF withAutoTF:(UITextField *)autoTF {
    
    
    __weak typeof(self)weakSelf = self;
    
    //http://bzg.ie1e.com/APP/User_UserLogin/DoFPwd?tel=18960821024&pwd=123&code=194724
    
    NSString *url = [NSString stringWithFormat:@"http://bzg.ie1e.com/APP/User_UserLogin/DoFPwd?tel=%@&pwd=%@&code=%@",tfNum.text,[Encryption md5EncryptWithString:passTF.text],autoTF.text];
    
    if (tfNum.text.length == 0) {
        [MBProgressHUD showError:@"手机号不能为空"];
        return;
    } else if (!([Encryption checkTelNumber:tfNum.text] == true)) {
        [MBProgressHUD showError:@"手机格式不对"];
        return;
    } else if(autoTF.text.length == 0) {
        [MBProgressHUD showError:@"验证码不能为空"];
        return;
    }else if (passTF.text.length == 0){
        [MBProgressHUD showError:@"密码不能为空"];
        return;
        
    }else if([Encryption checkTelNumber:tfNum.text] == true){
        
//        [MBManager showBriefAlert:@"修改中..."];
        [MBProgressHUD showMessage:@"修改中..."];
        [self.manager requestWithMethod:post andUrlString:url andParameters:nil andFinished:^(id response, NSError *error) {
            if (!error) {
                
                
                NSLog(@"%s  %d  %@",__func__,__LINE__,response);
                
                if ([response[@"resultmsg"] isEqualToString:@"找回成功"]) {
                    
                    [MBManager hideAlert];
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
                        [MBProgressHUD showSuccess:@"找回成功"];
                    });
                    
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
                        //代理回调方法
                        if ([self.delegate respondsToSelector:@selector(finishgetPassword:)]) {
                            [weakSelf.delegate finishgetPassword:self];
                        }
                    });

                } else if([response[@"resultmsg"] isEqualToString:@"短信套餐失效"]) {
                    [MBProgressHUD showError:@"出现未知错误"];
                } else if ([response[@"resultmsg"] isEqualToString:@"没有发送短信验证码"]){
                    [MBManager hideAlert];
                    [MBProgressHUD showError:@"没有发送短信验证码"];
                } else if([response[@"resultmsg"] isEqualToString:@"不存在该用户"]){
                    [MBManager hideAlert];
                    [MBProgressHUD showError:@"该号码未注册"];
                } else if([response[@"resultmsg"] isEqualToString:@"验证码错误"]) {
                    [MBManager hideAlert];
                    [MBProgressHUD showError:@"验证码错误"];
                }
            }
            
            if (error) {
                [MBManager hideAlert];
                [MBProgressHUD showError:@"网络异常"];
            }
        }];
        
    } else {
        [MBProgressHUD showError:@"手机格式不对"];
    }
}

- (void)rigisterSELWithTFNum:(UITextField *)tfNum withTFPass:(UITextField *)tfPass withTFAuto:(UITextField *)tfAuto {
    
    BOOL status = [stan boolForKey:rememChoose];
    
    NSLog(@"%s  %d  %zd",__func__,__LINE__,status);
    
    //http://bzg.ie1e.com/APP/User_UserLogin/Reg?eid=100001&na=昵称&pu=头像&tel=手机&pwd=密码&code=验证码
    //    http://bzg.ie1e.com/APP/User_UserLogin/Reg?eid=100001&tel=13665041134&pwd=123456&code=797551
    
    if (tfNum.text.length == 0) {
        [MBProgressHUD showError:@"手机号不能为空"];
        return;
    } else if(!([Encryption checkTelNumber:tfNum.text] == true)){
        [MBProgressHUD showError:@"手机格式不对"];
        return;
    } else if(tfPass.text.length == 0) {
        [MBProgressHUD showError:@"密码不能为空"];
        return;
    } else if(tfAuto.text.length == 0) {
        [MBProgressHUD showError:@"验证码不能为空"];
        return;
    } else if([Encryption checkTelNumber:tfNum.text] == true && status == 0) {
        
        NSString *url = [NSString stringWithFormat:@"http://bzg.ie1e.com/APP/User_UserLogin/Reg?tel=%@&pwd=%@&code=%@&ly=ios",tfNum.text,[Encryption md5EncryptWithString:tfPass.text],tfAuto.text];
        
        NSLog(@"%s  %d  %@",__func__,__LINE__,url);
        
        
//        [MBManager showBriefAlert:@"注册中..."];
        [MBProgressHUD showMessage:@"注册中..."];
        [self.manager requestWithMethod:post andUrlString:url andParameters:nil andFinished:^(id response, NSError *error) {
            NSLog(@"%s  %d  %@",__func__,__LINE__,response);
            
            if (!error) {

                if ([response[@"resultmsg"] isEqualToString:@"手机号已注册"]) {
                    [MBManager hideAlert];
                    [MBProgressHUD showError:@"手机号已注册"];
                    
                } else if ([response[@"resultmsg"] isEqualToString:@"注册成功"]) {
                    [MBManager hideAlert];
                    [MBProgressHUD showSuccess:@"注册成功"];
                    
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
                        if ([self.delegate respondsToSelector:@selector(finishSetRigster:)]) {
                            [self.delegate finishSetRigster:self];
                        }
                    });

                    
                    
                    
                }  else if ([response[@"resultmsg"] isEqualToString:@"没有发送短信验证码"]) {
                    [MBManager hideAlert];
                    [MBProgressHUD showError:@"没有发送短信验证码"];
                } else {
                    [MBManager hideAlert];
                    [MBProgressHUD showError:@"验证码不对"];
                }
                
            } else {
                [MBManager hideAlert];
                [MBProgressHUD showError:@"网络异常"];
            }
        }];
        
        
    
    
    } else{
        [MBProgressHUD showError:@"请同意协议"];
    }
}





@end
